package com.itianshan.wheel;


import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.liteav.chat.ChatRoomClass;

import java.io.UnsupportedEncodingException;

public class CloudWheelActivity extends CloudBaseActivity {
    private int                             mGrantedCount = 0;                           // 权限个数计数，获取Android系统权限
    private static final String             deviceType = "cloud_wheel";          //设备类型，bluetooth 和 usb
    private LinearLayout linerLayout;
    private WheelView wheelView_01,wheelView_02;
    private int speed;
    private int direction;
    private TextView textView_01,textView_02,send_count;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
        super.onCreate(savedInstanceState);
        if (checkPermission()) {
            initView(); //配置外观
            enterCoudWheelRoom(); //蓝牙操作
        }
    }
    private void enterCoudWheelRoom(){
        TextView roomState = findViewById(R.id.connection_room_state);
        roomState.setText("");
        chatRoomClass = new ChatRoomClass(mRoomId,mUserId,mIsFrontCamera);
        chatRoomClass.setLocalPreviewView(mLocalPreviewView); //传入远端用户显示用到的控件
        chatRoomClass.setRemoteUserViews(mRemoteViewList); //传入远端用户显示用到的控件
        chatRoomClass.setCloudType(deviceType); //用于判断接收到云小车的消息

        chatRoomClass.prepareToEnterRoom(getApplicationContext(),roomState);  //准备进入房间，服务器获取签名
    }
    @Override
    public void initView() {
        super.initView();

        linerLayout = findViewById(R.id.control_wheel);
        linerLayout.setVisibility(View.VISIBLE);

        send_count=(TextView) findViewById(R.id.send_count);
        wheelView_01 = (WheelView) findViewById(R.id.wheelView_01);
        textView_01 = (TextView) findViewById(R.id.textView_01);
        wheelView_02 = (WheelView) findViewById(R.id.wheelView_02);
        textView_02 = (TextView) findViewById(R.id.textView_02);

        wheelView_01.setOnWheelViewMoveListener(
                new WheelView.OnWheelViewMoveListener() {
                    @Override
                    public void onValueChanged(int angle, int distance) {
                        textView_01.setText("角度：" + angle + "\n" +	"距离：" + distance);
                        if(angle<-115 && -180<angle){
                            angle = -(180+angle);
                        }
                        if (115<angle && angle<180){
                            angle = 180-angle;
                        }
                        if(!(angle>-65 && angle <65)){
                            angle = direction;
                        }
                        String dir = angle==0?"":(angle>0?"右":"左");
                        textView_01.setText("方向：" +dir+ Math.abs(angle) + "\n");

                        String command = "{\"dir\":"+String.valueOf(angle)+"}\r\n";
                        //通过视频通话发送控制命令
                        try {
                            // 自定义消息命令字, 这里需要根据业务定制一套规则，这里以0x1代表发送文字广播消息为例
                            int cmdID = 0x1;
                            byte[] data = command.getBytes("UTF-8");
                            // reliable 和 ordered 目前需要一致，这里以需要保证消息按发送顺序到达为例
                            long ts = System.currentTimeMillis();
                            chatRoomClass.mTRTCCloud.sendCustomCmdMsg(cmdID, data, true, true);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                        direction = angle;
                    }
                }, 300L);
        wheelView_02.setOnWheelViewMoveListener(
                new WheelView.OnWheelViewMoveListener() {
                    @Override
                    public void onValueChanged(int angle, int distance) {
                        //textView_02.setText("角度：" + angle + "\n" +	"距离：" + distance);
                        if(!(angle>-95 && angle <95)){
                            distance = -distance/10;
                        }
                        textView_02.setText("速度：" + distance+"%"+(distance==0?"":(distance>0?"前进":"倒车")));
                        String command = "{\"spe\":"+String.valueOf(distance)+"}\r\n";
                        //通过视频通话发送控制命令
                        try {
                            // 自定义消息命令字, 这里需要根据业务定制一套规则，这里以0x1代表发送文字广播消息为例
                            int cmdID = 0x1;
                            byte[] data = command.getBytes("UTF-8");
                            // reliable 和 ordered 目前需要一致，这里以需要保证消息按发送顺序到达为例
                            long ts = System.currentTimeMillis();
                            chatRoomClass.mTRTCCloud.sendCustomCmdMsg(cmdID, data, true, true);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                        speed = distance;
                    }
                }, 300L);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQ_PERMISSION_CODE) {
            for (int ret : grantResults) {
                if (PackageManager.PERMISSION_GRANTED == ret) mGrantedCount++;
            }
            if (mGrantedCount == permissions.length) {
                //首次启动，权限都获取到，才能正常进入通话。
                enterCoudWheelRoom();
            } else {
                Toast.makeText(this, getString(R.string.rtc_permisson_error_tip), Toast.LENGTH_SHORT).show();
            }
            mGrantedCount = 0;
        }
    }

    @Override
    protected void onDestroy() {
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//恢复竖屏
        super.onDestroy();
    }
}
